var element, laydate, form, table, contactPie, contactLine, idLine, idPie, currentModifyType, currentStatsType,
    currentInfoType;
var contactStatsIndex = 0;
var idStatsIndex = 0;

var contactModifyIndex = 0;
var contactAddIndex = 0;
var idModifyIndex = 0;
var idAddIndex = 0;


var currentModifyType = "contactInfo";

var contactModifyConfig = {
    tableId: "contactModifyTable",
    tableCols: [
        {field: 'id', hide: true}
        , {field: 'tableId', title: '序号', type: 'numbers'}
        , {field: 'customerNo', title: '客户号', align: 'center'}
        , {field: 'contactType', title: '联系方式类型', align: 'center'}
        , {field: 'contactInfo', title: '联系方式', align: 'center'}
        , {field: 'contactTypeOld', title: '原联系方式类型', align: 'center'}
        , {field: 'contactInfoOld', title: '原联系方式', align: 'center'}
        , {field: 'createTime', title: '创建时间', align: 'center'}
    ],
};

var contactAddConfig = {
    tableId: "contactAddTable",
    tableCols: [
        {field: 'id', hide: true}
        , {field: 'tableId', title: '序号', type: 'numbers'}
        , {field: 'customerNo', title: '客户号', align: 'center'}
        , {field: 'contactType', title: '联系方式类型', align: 'center'}
        , {field: 'contactInfo', title: '联系方式', align: 'center'}
        , {field: 'systemSign', title: '系统标识', align: 'center'}
        , {field: 'subSystemSign', title: '子系统标识', align: 'center'}
        , {field: 'createTime', title: '创建时间', align: 'center'}
    ],
};

var idModifyConfig = {
    tableId: "idModifyTable",
    tableCols: [
        {field: 'id', hide: true},
        {type: 'checkbox', width: 50}
        , {field: 'tableId', title: '序号', type: 'numbers'}
        , {field: 'customerNo', title: '客户号', align: 'center'}
        , {field: 'customerName', title: '客户姓名', align: 'center'}
        , {field: 'idCardType', title: '证件类型', align: 'center'}
        , {field: 'idCardNo', title: '证件号码', align: 'center'}
        , {field: 'customerNameOld', title: '原客户姓名', align: 'center'}
        , {field: 'idCardTypeOld', title: '原证件类型', align: 'center'}
        , {field: 'idCardNoOld', title: '原证件号码', align: 'center'}
        , {field: 'noticeStatus', title: '通知状态', align: 'center'}
        , {field: 'createTime', title: '创建时间', align: 'center'}
        , {field: 'subSystemSign', title: '子系统标识', align: 'center'}
    ],
};


var idAddConfig = {
    tableId: "idAddTable",
    tableCols: [
        {field: 'id', hide: true}
        , {field: 'tableId', title: '序号', type: 'numbers'}
        , {field: 'customerNo', title: '客户号', align: 'center'}
        , {field: 'customerName', title: '客户姓名', align: 'center'}
        , {field: 'idCardType', title: '证件类型', align: 'center'}
        , {field: 'idCardNo', title: '证件号码', align: 'center'}
        , {field: 'elementsVerified', title: '三要素核查结果', align: 'center'}
        , {field: 'systemSign', title: '系统标识', align: 'center'}
        , {field: 'subSystemSign', title: '子系统标识', align: 'center'}
        , {field: 'createTime', title: '创建时间', align: 'center'}
    ],
};

var queryListData = function (id, cols, data) {
    table.render({
        elem: "#" + id,
        height: 340,
        cols: [cols],
        url: serverPre + "/manager/yzt/v1/customerModify",
        method: 'post',
        headers: {"token": getCookie("token")},
        where: {ro: data},
        page: true,
        limits: [10, 20, 50],
        limit: 20,
        parseData: function (res) {
            checkLogin(res);
            if (res.code == 0) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.pageInfo.totalCount, //解析数据长度
                    "data": res.data.pageInfo.list //解析数据列表
                };
            } else {
                return {
                    "msg": res.msg, //解析提示文本
                };
            }
        },
    });
};

var showLineStats = function (type, dateType, statsType) {
    $.ajax({
        type: 'post',
        headers: {"token": getCookie("token")},
        url: serverPre + "/manager/yzt/v1/customerModify/stats",
        data: {type: type, dateType: dateType, statsType: statsType},
        async: false,
        cache: false,
        success: function (result) {
            if (result.code == 0) {
                if (type.startsWith("contact")) {
                    var a = JSON.parse(result.data.contactLine)
                    a.color = ["#7ec8c1", "#49b2c3"];
                    contactLine.setOption(a);
                    if (!statsType) {
                        contactPie.setOption(JSON.parse(result.data.contactPie))
                    }
                } else {
                    idLine.setOption(JSON.parse(result.data.idLine));
                    if (!statsType) {
                        idPie.setOption(JSON.parse(result.data.idPie))
                    }
                }
            }
        }
    });
};
var selectedData = [];

$(function () {
    layui.use(['element', 'laydate', 'form', 'table'], function () {
        element = layui.element;
        laydate = layui.laydate;
        form = layui.form;
        table = layui.table;
        lay('.contact-date').each(function () {
            laydate.render({
                elem: this
                , trigger: 'click'
                , type: 'datetime'
            });
        });

        element.on('tab', function (data) {
            var id = this.getAttribute('lay-id');
            if (id) {
                if (id.split("-").length === 1) {
                    //联系信息与证件信息之间切换
                    currentModifyType = id;
                    if (!id.startsWith("contact")) {
                        if (idStatsIndex == 0) {
                            idLine = echarts.init(document.getElementById('idLine'));
                            idPie = echarts.init(document.getElementById('idPie'));
                            showLineStats(id, "week");
                            idStatsIndex = 1;
                        }
                        if (idModifyIndex == 0) {
                            queryListData(idModifyConfig.tableId, idModifyConfig.tableCols, JSON.stringify({
                                modifyType: "id",
                                modifyMethod: "modify"
                            }));
                            idModifyIndex = 1;
                        }
                    }
                } else if (id.split("-").length === 2) {
                    //表格信息在新增与变更之间切换
                    currentInfoType = id.split("-")[1];
                    if (!currentModifyType.startsWith("contact")) {
                        //证件信息
                        if (currentInfoType === 'add') {
                            //增加
                            if (idAddIndex == 0) {
                                queryListData(idAddConfig.tableId, idAddConfig.tableCols, JSON.stringify({
                                    modifyType: "id",
                                    modifyMethod: "add"
                                }));
                                idAddIndex = 1;
                            }
                        } else {
                            //修改

                        }
                    } else {
                        //联系信息
                        if (currentInfoType === 'add') {
                            //增加
                            if (contactAddIndex == 0) {
                                queryListData(contactAddConfig.tableId, contactAddConfig.tableCols, JSON.stringify({
                                    modifyType: "contact",
                                    modifyMethod: "add"
                                }));
                                contactAddIndex = 1;
                            }
                        } else {
                            //修改
                            if (contactModifyIndex == 0) {
                                queryListData(contactModifyConfig.tableId, contactModifyConfig.tableCols, JSON.stringify({
                                    modifyType: "contact",
                                    modifyMethod: "modify"
                                }));
                                contactModifyIndex = 1;
                            }
                        }
                    }
                } else if (id.split("-").length === 3) {
                    currentStatsType = id.split("-")[2];
                    //统计报表month与week之间切换
                    if (currentModifyType.startsWith("contact")) {
                        showLineStats("contactInfo", currentStatsType, 'line');
                    } else {
                        showLineStats("idInfo", currentStatsType, 'line');
                    }
                }
            }
            return false;
        });

        form.on('submit', function (data) {
            var id = data.form.id;
            if (id === "contactModifyForm") {
                data.field.modifyType = "contact";
                data.field.modifyMethod = "modify";
                queryListData(contactModifyConfig.tableId, contactModifyConfig.tableCols, JSON.stringify(data.field));
            } else if (id === "contactAddForm") {
                data.field.modifyType = "contact";
                data.field.modifyMethod = "add";
                queryListData(contactAddConfig.tableId, contactAddConfig.tableCols, JSON.stringify(data.field));
            } else if (id === "idModifyForm") {
                data.field.modifyType = "id";
                data.field.modifyMethod = "modify";
                queryListData(idModifyConfig.tableId, idModifyConfig.tableCols, JSON.stringify(data.field));
            } else if (id === "idAddForm") {
                data.field.modifyType = "id";
                data.field.modifyMethod = "add";
                queryListData(idAddConfig.tableId, idAddConfig.tableCols, JSON.stringify(data.field));
            }
            return false;
        });

        contactLine = echarts.init(document.getElementById('contactLine'));
        contactPie = echarts.init(document.getElementById('contactPie'));
        showLineStats("contactInfo", "week");
        contactStatsIndex = 1;
        queryListData(contactModifyConfig.tableId, contactModifyConfig.tableCols, JSON.stringify({
            modifyType: "contact",
            modifyMethod: "modify"
        }));
        contactModifyIndex = 1;
    });

    $('#noticeBtn').on('click',function () {
        var checkStatus = table.checkStatus('idModifyTable');
        var selectedData = checkStatus.data;
        noticeBiz(selectedData);
        // queryData(url, pageObj.curr, pageObj.limit);
        return false;
    });
});

var noticeBiz = function (selectedData) {
    if (selectedData.length != 1) {
        layer.msg('请选择一条数据');
        return;
    } else if (selectedData[0]['noticeStatus'] == "通知成功") {
        layer.msg('当前状态已通知成功');
        return;
    }
    var bizId = selectedData[0].id;
    var customerNo = selectedData[0].customerNo;
    var customerName = selectedData[0].customerName;
    var idCardType = selectedData[0].idCardType;
    var idCardNo = selectedData[0].idCardNo;
    var type = 1;
    $.ajax({
        type: 'get',
        url: serverPre + "/manager/yzt/v1/customer/sendBizNotice?id=" + bizId + "&&customerNo=" + customerNo + "&&customerName=" + customerName + "&&idCardType=" + idCardType + "&&idCardNo=" + idCardNo + "&&type=" + type,
        dataType: 'json',
        headers: {"token": getCookie("token")},
        async: false,
        cache: false,
        success: function (result) {
            if (result.code == 0 && result.data.status == 0) {
                layer.msg("通知成功", {
                    icon: 1,
                    time: 1000
                });
                queryListData(idModifyConfig.tableId, idModifyConfig.tableCols, JSON.stringify({
                    modifyType: "id",
                    modifyMethod: "modify"
                }));
            } else {
                layer.msg("通知失败", {
                    time: 2000
                });
            }
        }
    });
};

function modify_search() {
    $("#contactModifyForm").toggle()
}

function idmodify_search() {
    $("#idModifyForm").toggle()
}

function add_search() {
    $("#contactAddForm").toggle()
}

function idAdd_search() {
    $("#idAddForm").toggle()
}
